package com.info.zhiduoduo.repository.mapper.quota;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.mapper.Condition;
import com.info.zhiduoduo.repository.dto.quota.ZhiduoduoCityQuotaDto;
import com.info.zhiduoduo.repository.entity.quota.ZhiduoduoCityQuota;

import org.apache.ibatis.annotations.Select;

public interface ZhiduoduoCityQuotaMapper extends BaseMapper<ZhiduoduoCityQuota> {

	default boolean deleteByConfigId(int configId) {
		return new ZhiduoduoCityQuota().delete(Condition.create().eq("config_id", configId));
	}

	default boolean insertOrUpdateByConfigId(ZhiduoduoCityQuota zhiduoduoCityQuota) {
		boolean update = zhiduoduoCityQuota.update(Condition.create().eq("config_id", zhiduoduoCityQuota.getConfigId()));
		if (!update) return zhiduoduoCityQuota.insert();
		return true;
	}

	@Select(" select * from zdd_zhiduoduo_city_quota where config_id = #{configId} order by id desc limit 1 ")
	ZhiduoduoCityQuotaDto getByConfigId(Integer configId);

	@Select(" select zcq.* from zdd_zhiduoduo_city_quota zcq " +
			" join zdd_maker_account ma on zcq.config_id = ma.opening_location" +
			" where ma.userid = #{userId} " +
			" order by id desc limit 1 ")
	ZhiduoduoCityQuotaDto getByMakerId(Integer userId);

}
